home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 60750 / 60750.xpi / surfcanyon_sc.xpi / content / script-compiler.js < prev    next >
Text File  |  2009-11-12  |  15KB  |  460 lines

  1. function SurfCanyonMisc() {
  2. }
  3.  
  4. SurfCanyonMisc.prototype.log = function(text) {
  5.     Components.classes["@mozilla.org/consoleservice;1"].
  6.         getService(Components.interfaces.nsIConsoleService).
  7.         logStringMessage(text);
  8. }
  9.  
  10. SurfCanyonMisc.prototype.updateWebOfTrust = function(node) {
  11.     if (typeof(wot_search) == "object" && typeof(wot_search.process) == "function") {
  12.         wot_search.process(node);
  13.     }
  14. }
  15.  
  16. SurfCanyonMisc.prototype.addToSearchBar = function(name, openSearchUrl) {
  17.     var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService().QueryInterface(Components.interfaces.nsIBrowserSearchService);
  18.     if (searchService.getEngineByName(name) == null) {
  19.         searchService.addEngine(openSearchUrl, Components.interfaces.nsISearchEngine.DATA_XML, null, false);
  20.     }
  21. }
  22.  
  23. SurfCanyonMisc.prototype.openNewTab = function(url) {
  24.     var watcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService().QueryInterface(Components.interfaces.nsIWindowWatcher);
  25.     var list = watcher.getWindowEnumerator();
  26.     while (list.hasMoreElements()) {
  27.         var w = list.getNext().QueryInterface(Components.interfaces.nsIDOMWindow);
  28.         try {
  29.             var win = w.QueryInterface(Components.interfaces.nsIDOMWindowInternal);
  30.             if (win.location.href == "chrome://browser/content/browser.xul") {
  31.                 var browser = win.getBrowser();
  32.                 browser.selectedTab = browser.addTab(url);
  33.                 return;
  34.             }
  35.         } catch(e) {
  36.         }
  37.     }
  38. }
  39.  
  40. function getSurfCanyonPrefsBranch() {
  41.     return Components.classes["@mozilla.org/preferences-service;1"].
  42.         getService(Components.interfaces.nsIPrefService).
  43.         getBranch("surfcanyon.");
  44. }
  45.  
  46. function scNotificationBoxButtonCallback(arg) {
  47.     var notificationBox = getBrowser().getNotificationBox();
  48.     notificationBox.removeAllNotifications();
  49.  
  50.     var prefsBranch = getSurfCanyonPrefsBranch();
  51.     try {
  52.         var notificationNum = prefsBranch.getIntPref('last_notification_displayed');
  53.         if (notificationNum == 1) {
  54.             prefsBranch.setBoolPref('recs_notifications_cancelled', true);
  55.         } else if (notificationNum == 2) {
  56.             prefsBranch.setBoolPref('page_notifications_cancelled', true);
  57.         }
  58.     } catch (e) {
  59.     }
  60. }
  61.  
  62. SurfCanyonMisc.prototype.showNotification = function(msg, notificationNum) {
  63.     var prefsBranch = getSurfCanyonPrefsBranch();
  64.     var cancelled = false;
  65.     try {
  66.         if (notificationNum == 1) {
  67.             cancelled = prefsBranch.getBoolPref('recs_notifications_cancelled');
  68.         } else if (notificationNum == 2) {
  69.             cancelled = prefsBranch.getBoolPref('page_notifications_cancelled');
  70.         }
  71.     } catch (e) {
  72.     }
  73.  
  74.     if (!cancelled) {
  75.         var buttons = [{
  76.             label: "Don't show this message again",
  77.             accessKey: null, popup: null,
  78.             callback: scNotificationBoxButtonCallback
  79.         }];
  80.         var notificationBox = getBrowser().getNotificationBox();
  81.         notificationBox.removeAllNotifications();
  82.         notificationBox.appendNotification(msg, "scMessage", null, notificationBox.PRIORITY_WARNING_HIGH, buttons);
  83.  
  84.         prefsBranch.setIntPref('last_notification_displayed', notificationNum);
  85.     }
  86. }
  87.  
  88. SurfCanyonMisc.prototype.getBoolPreferenceValue = function(key) {
  89.     var val = false;
  90.     try {
  91.         val = getSurfCanyonPrefsBranch().getBoolPref(key);
  92.     } catch (e) {
  93.     }
  94.     return val;
  95. }
  96.  
  97. SurfCanyonMisc.prototype.setBoolPreferenceValue = function(key, value) {
  98.     getSurfCanyonPrefsBranch().setBoolPref(key, value);
  99. }
  100.  
  101. SurfCanyonMisc.prototype.getCharPreferenceValue = function(key) {
  102.     var val = null;
  103.     try {
  104.         val = getSurfCanyonPrefsBranch().getCharPref(key);
  105.     } catch (e) {
  106.     }
  107.     return val;
  108. }
  109.  
  110. SurfCanyonMisc.prototype.setCharPreferenceValue = function(key, value) {
  111.     getSurfCanyonPrefsBranch().setCharPref(key, value);
  112. }
  113.  
  114. SurfCanyonMisc.prototype.getIntPreferenceValue = function(key) {
  115.     var val = null;
  116.     try {
  117.         val = getSurfCanyonPrefsBranch().getIntPref(key);
  118.     } catch (e) {
  119.     }
  120.     return val;
  121. }
  122.  
  123. SurfCanyonMisc.prototype.setIntPreferenceValue = function(key, value) {
  124.     getSurfCanyonPrefsBranch().setIntPref(key, value);
  125. }
  126.  
  127. SurfCanyonMisc.prototype.optionsInit = function() {
  128.     try {
  129.         var elem = document.getElementById("sc.disabled");
  130.         elem.checked = !this.getBoolPreferenceValue("disabled");
  131.     
  132.         elem = document.getElementById("sc.statusBarIconDisabled");
  133.         elem.checked = !this.getBoolPreferenceValue("status_bar_icon_disabled");
  134.     } catch (e) {
  135.         alert(e.message);
  136.     }
  137. }
  138.  
  139. SurfCanyonMisc.prototype.optionsSave = function() {
  140.     var changed = false;
  141.  
  142.     var statusBarIconWasDisabled = false;
  143.     try {
  144.         statusBarIconWasDisabled = this.getBoolPreferenceValue("status_bar_icon_disabled");
  145.     } catch (e1) {
  146.     }
  147.     
  148.     var statusBarIconDisabled;
  149.     try {
  150.         var elem1 = document.getElementById("sc.statusBarIconDisabled");
  151.         statusBarIconDisabled = !elem1.checked;
  152.         if (statusBarIconDisabled != statusBarIconWasDisabled) {
  153.             this.setBoolPreferenceValue("status_bar_icon_disabled", statusBarIconDisabled);
  154.             changed = true;
  155.         }
  156.     } catch (e2) {
  157.         alert(e2.message);
  158.     }
  159.  
  160.     var wasDisabled = false;
  161.     try {
  162.         wasDisabled = this.getBoolPreferenceValue("disabled");
  163.     } catch (e3) {
  164.     }
  165.  
  166.     var disabled;
  167.     try {
  168.         var elem2 = document.getElementById("sc.disabled");
  169.         disabled = !elem2.checked;
  170.         if (disabled != wasDisabled) {
  171.             this.setBoolPreferenceValue("disabled", disabled);
  172.             changed = true;
  173.         }
  174.     } catch (e4) {
  175.         alert(e4.message);
  176.     }
  177.  
  178.     if (changed) {
  179.         try {
  180.             this.updateDisabledStatusInUI(disabled, statusBarIconDisabled); // might reload page
  181.         } catch (e4) {
  182.             alert(e4);
  183.         }
  184.     }
  185.  
  186.     return true;
  187. }
  188.  
  189. function getSCStatusNode()  {
  190.     var doc = (window.opener) ? window.opener.document : document;
  191.     return doc.getElementById('surfcanyon-status');
  192. }
  193.  
  194. function getSCUrlBarNode() {
  195.     var doc = (window.opener) ? window.opener.document : document;
  196.     return doc.getElementById('surfcanyon-urlbar-main');
  197. }
  198.  
  199. SurfCanyonMisc.prototype.updateDisabledStatusInUI = function(disabled, statusBarIconDisabled) {
  200.     var statusNode = getSCStatusNode();
  201.     
  202.     if (statusNode) {
  203.         if (statusBarIconDisabled != 'x') {
  204.             statusNode.style.visibility = statusBarIconDisabled ? "collapse" : "visible";
  205.         }
  206.         if (disabled != 'x') {
  207.             statusNode.setAttribute("status", disabled ? "0" : "1");
  208.             window.opener._content.document.location.reload();
  209.         }
  210.     }
  211. }
  212.  
  213. SurfCanyonMisc.prototype.removeFromUrlBar = function() {
  214.     var urlBarNode = getSCUrlBarNode();
  215.     urlBarNode.style.display = 'none';
  216.     this.setBoolPreferenceValue('url_bar_icon_disabled', true); 
  217.     _content.document.location.reload();
  218. }
  219.  
  220. SurfCanyonMisc.prototype.reload = function() {
  221.     _content.document.location.reload();
  222. }
  223.  
  224. SurfCanyonMisc.prototype.isGooglePreviewOn = function() {
  225.     return window && window.com && window.com.googlepreview &&
  226.         window.com.googlepreview.package &&
  227.         window.com.googlepreview.package.ENABLE_IMAGE_INSERT_PREF &&
  228.         Components.classes["@mozilla.org/preferences-service;1"].
  229.         getService(Components.interfaces.nsIPrefBranch).
  230.         getBranch("googlepreview.").getBoolPref('insertimages');
  231. }
  232.  
  233. var surfcanyon_gmCompiler={
  234. observe: function(p_Subject, p_Topic, p_Data) {
  235.     if (p_Data == "item-uninstalled") {
  236.         p_Subject.QueryInterface(Components.interfaces.nsIUpdateItem);
  237.         if (p_Subject.id == "{75623d5d-4683-402a-b610-ac4bab767c86}") {
  238.             try {
  239.                 var searchService = Components.classes["@mozilla.org/browser/search-service;1"].getService().QueryInterface(Components.interfaces.nsIBrowserSearchService);
  240.                 var engine = searchService.getEngineByName('Surf Canyon');
  241.                 if (engine) {
  242.                     searchService.removeEngine(engine);
  243.                 }
  244.             } catch (e1) {
  245.             }
  246.  
  247.             var url = "http://www.surfcanyon.com/search/uninstall.jsp";
  248.             try {
  249.                 var prefsBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch).getBranch("surfcanyon.");
  250.                 var partnerCode = prefsBranch.getCharPref("partner_code");
  251.                 if (partnerCode && (partnerCode.toLowerCase() != 'oneriot')) {
  252.                     url = url + "?instId=" + partnerCode + prefsBranch.getCharPref("inst_id");
  253.                 } else {
  254.                     url = null;
  255.                 }
  256.             } catch (e2) {
  257.             }
  258.  
  259.             if (url) {
  260.                 getBrowser().addTab(url);
  261.             }
  262.         }
  263.     }
  264. },
  265.  
  266. activateMonitor: function() {
  267.     var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  268.     observerService.addObserver(this, "em-action-requested", false);
  269. },
  270.  
  271. deactivateMonitor: function() {
  272.     var observerService = Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
  273.     observerService.removeObserver(this, "em-action-requested");
  274. },
  275.  
  276. getUrlContents: function(aUrl){
  277. // getUrlContents adapted from Greasemonkey Compiler
  278. // http://www.letitblog.com/code/python/greasemonkey.py.txt
  279. // used under GPL permission
  280. //
  281. // most everything else below based heavily off of Greasemonkey
  282. // http://greasemonkey.mozdev.org/
  283. // used under GPL permission
  284.     var    ioService=Components.classes["@mozilla.org/network/io-service;1"]
  285.         .getService(Components.interfaces.nsIIOService);
  286.     var    scriptableStream=Components
  287.         .classes["@mozilla.org/scriptableinputstream;1"]
  288.         .getService(Components.interfaces.nsIScriptableInputStream);
  289.  
  290.     var    channel=ioService.newChannel(aUrl, null, null);
  291.     var    input=channel.open();
  292.     scriptableStream.init(input);
  293.     var    str=scriptableStream.read(input.available());
  294.     scriptableStream.close();
  295.     input.close();
  296.  
  297.     return str;
  298. },
  299.  
  300. contentLoad: function(e) {
  301.     if ((typeof autopagerMain != 'undefined') && autopagerMain && autopagerMain.isAutoPagerHiddenWindow && autopagerMain.isAutoPagerHiddenWindow(e.target)) {
  302.         return;
  303.     }
  304.     
  305.     var unsafeWin=e.target.defaultView;
  306.     if (unsafeWin.wrappedJSObject) unsafeWin=unsafeWin.wrappedJSObject;
  307.  
  308.     var unsafeLoc=new XPCNativeWrapper(unsafeWin, "location").location;
  309.     var href=new XPCNativeWrapper(unsafeLoc, "href").href;
  310.  
  311.     if (/^http:/.test(href)) {
  312.         var prefsBranch = getSurfCanyonPrefsBranch();
  313.  
  314.         try {
  315.             var statusNode = getSCStatusNode();
  316.             if (statusNode) {
  317.                 var disabled;
  318.                 try {
  319.                     disabled = prefsBranch.getBoolPref('disabled');
  320.                 } catch (e1) {
  321.                 }
  322.  
  323.                 var statusBarIconDisabled;
  324.                 try {
  325.                     statusBarIconDisabled = prefsBranch.getBoolPref('status_bar_icon_disabled'); 
  326.                 } catch (e2) {
  327.                 }
  328.  
  329.                 statusNode.style.visibility = statusBarIconDisabled ? "collapse" : "visible";
  330.                 statusNode.setAttribute('status', (disabled ? '0' : '1')); 
  331.             }
  332.         } catch (e3) {
  333.         }
  334.  
  335.         try {
  336.             var urlBarNode = getSCUrlBarNode();
  337.  
  338.             var urlBarIconDisabled;
  339.             try {
  340.                 urlBarIconDisabled = prefsBranch.getBoolPref('url_bar_icon_disabled'); 
  341.             } catch (e4) {
  342.             }
  343.  
  344.             urlBarNode.style.display = urlBarIconDisabled ? 'none' : 'block';
  345.         } catch (e5) {
  346.         }
  347.  
  348.         var script=surfcanyon_gmCompiler.getUrlContents('chrome://surfcanyon/content/surfcanyon.js');
  349.         // script += "\n\n"+surfcanyon_gmCompiler.getUrlContents('chrome://surfcanyon/content/jquery.js');
  350.         surfcanyon_gmCompiler.injectScript(script, href, unsafeWin);
  351.     }
  352. },
  353.  
  354. injectScript: function(script, url, unsafeContentWin) {
  355.     var safeWin=new XPCNativeWrapper(unsafeContentWin);
  356.     var sandbox=new Components.utils.Sandbox(safeWin);
  357.  
  358.     sandbox.window=safeWin;
  359.     sandbox.document=sandbox.window.document;
  360.     sandbox.unsafeWindow=unsafeContentWin;
  361.  
  362.     // patch missing properties on xpcnw
  363.     sandbox.XPathResult=Components.interfaces.nsIDOMXPathResult;
  364.  
  365.     var xhr=new surfcanyon_xmlhttpRequester(unsafeContentWin, window);
  366.  
  367.     // add our own APIs
  368.     var scMisc = new SurfCanyonMisc();
  369.     sandbox.ajax=surfcanyon_gmCompiler.hitch(xhr, "contentStartRequest");
  370.     sandbox.log=surfcanyon_gmCompiler.hitch(scMisc, "log");
  371.     sandbox.updateWebOfTrust=surfcanyon_gmCompiler.hitch(scMisc, "updateWebOfTrust");
  372.     sandbox.addToSearchBar=surfcanyon_gmCompiler.hitch(scMisc, "addToSearchBar");
  373.     sandbox.openNewTab=surfcanyon_gmCompiler.hitch(scMisc, "openNewTab");
  374.     sandbox.showNotification=surfcanyon_gmCompiler.hitch(scMisc, "showNotification");
  375.     sandbox.getIntPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "getIntPreferenceValue");
  376.     sandbox.getBoolPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "getBoolPreferenceValue");
  377.     sandbox.setBoolPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "setBoolPreferenceValue");
  378.     sandbox.getCharPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "getCharPreferenceValue");
  379.     sandbox.setCharPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "setCharPreferenceValue");
  380.     sandbox.getIntPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "getIntPreferenceValue");
  381.     sandbox.setIntPreferenceValue=surfcanyon_gmCompiler.hitch(scMisc, "setIntPreferenceValue");
  382.     sandbox.updateDisabledStatusInUI=surfcanyon_gmCompiler.hitch(scMisc, "updateDisabledStatusInUI");
  383.     sandbox.reload=surfcanyon_gmCompiler.hitch(scMisc, "reload");
  384.     sandbox.isGooglePreviewOn=surfcanyon_gmCompiler.hitch(scMisc, "isGooglePreviewOn");
  385.  
  386.     sandbox.__proto__=sandbox.window;
  387.  
  388.     try {
  389.         this.evalInSandbox("(function(){"+script+"})()", url, sandbox);
  390.     } catch (e) {
  391.         var e2=new Error(typeof e=="string" ? e : e.message);
  392.         e2.fileName=script.filename;
  393.         e2.lineNumber=0;
  394.         alert(e2);
  395.     }
  396. },
  397.  
  398. evalInSandbox: function(code, codebase, sandbox) {
  399.     if (Components.utils && Components.utils.Sandbox) {
  400.         // DP beta+
  401.         Components.utils.evalInSandbox(code, sandbox);
  402.     } else if (Components.utils && Components.utils.evalInSandbox) {
  403.         // DP alphas
  404.         Components.utils.evalInSandbox(code, codebase, sandbox);
  405.     } else if (Sandbox) {
  406.         // 1.0.x
  407.         evalInSandbox(code, sandbox, codebase);
  408.     } else {
  409.         throw new Error("Could not create sandbox.");
  410.     }
  411. },
  412.  
  413. hitch: function(obj, meth) {
  414.     if (!obj[meth]) {
  415.         throw "method '" + meth + "' does not exist on object '" + obj + "'";
  416.     }
  417.  
  418.     var staticArgs = Array.prototype.splice.call(arguments, 2, arguments.length);
  419.  
  420.     return function() {
  421.         // make a copy of staticArgs (don't modify it because it gets reused for
  422.         // every invocation).
  423.         var args = staticArgs.concat();
  424.  
  425.         // add all the new arguments
  426.         for (var i = 0; i < arguments.length; i++) {
  427.             args.push(arguments[i]);
  428.         }
  429.  
  430.         // invoke the original function with the correct this obj and the combined
  431.         // list of static and dynamic arguments.
  432.         return obj[meth].apply(obj, args);
  433.     };
  434. },
  435.  
  436. onLoad: function() {
  437.     surfcanyon_gmCompiler.activateMonitor();
  438.     var appcontent=window.document.getElementById("appcontent");
  439.     if (appcontent && !appcontent.greased_surfcanyon_gmCompiler) {
  440.         appcontent.greased_surfcanyon_gmCompiler=true;
  441.         appcontent.addEventListener("DOMContentLoaded", surfcanyon_gmCompiler.contentLoad, false);
  442.     }
  443. },
  444.  
  445. onUnLoad: function() {
  446.     surfcanyon_gmCompiler.deactivateMonitor();
  447.     window.removeEventListener('load', surfcanyon_gmCompiler.onLoad, false);
  448.     window.removeEventListener('unload', surfcanyon_gmCompiler.onUnLoad, false);
  449.     window.document.getElementById("appcontent")
  450.         .removeEventListener("DOMContentLoaded", surfcanyon_gmCompiler.contentLoad, false);
  451. },
  452.  
  453. }; //object surfcanyon_gmCompiler
  454.  
  455.  
  456. window.addEventListener('load', surfcanyon_gmCompiler.onLoad, false);
  457. window.addEventListener('unload', surfcanyon_gmCompiler.onUnLoad, false);
  458.  
  459.  
  460.